import { View } from "../../../mvc/core/View";
import { NetDispatcher } from "../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../mvc/ServerCommand";
import { SkillInfo } from "../../model/SkillInfo";
import { ShortcutsItem } from "./ShortcutsItem";
import { GameConst } from "../../../component/gconst/GameConst";
import { DisplayUtil } from "../../../common/DisplayUtil";
import { CDItemData } from "../../cache/CDItem/CDItemData";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { Cache } from "../../cache/Cache";
import { ESkillType } from "../../../../Message/Public/ESkillType";
type int = number;
//class NavSkillPanel
    
    export  class NavSkillPanel extends View
    {
        constructor()
        {
            super();
            this._skillItemDict = new Map<any, any> /* flash.utils.Dictionary */();
            NetDispatcher.addCmdListener(ServerCommand.BufferUpdate, this.onBuffUpdate);
            return;
        }

        public addSkill(arg1: SkillInfo): ShortcutsItem
        {
            var loc1=null;
            var loc3=null;
            var loc2=arg1.skillId;
            if (this._skillItemDict[loc2] != null) 
            {
                (loc3 = this._skillItemDict[loc2] as ShortcutsItem).dragSource = arg1;
            }
            else 
            {
                loc1 = new ShortcutsItem();
                loc1.isDragAble = false;
                loc1.isDropAble = false;
                loc1.isShowBg = false;
                loc1.dragSource = arg1;
                loc1.x = this._amount * 38 + 10;
                if (arg1.tSkill.skillId == GameConst.CountryBuffSkillId || arg1.tSkill.skillId == GameConst.CountryCallTogetherKillId) 
                {
                    loc1.isDragAble = false;
                }
                var loc4;
                var loc5=((loc4 = this)._amount + 1);
                loc4._amount = loc5;
                this.addChild(loc1);
                this._skillItemDict[loc2] = loc1;
            }
            this.resetSkillIconPlace();
            return loc1;
        }

        public removeSkill(arg1: SkillInfo): void
        {
            if (this._skillItemDict == null) 
            {
                return;
            }
            var loc1=this._skillItemDict[arg1.skillId] as ShortcutsItem;
            this._skillItemDict[arg1.skillId] = null;
            delete this._skillItemDict[arg1.skillId];
            DisplayUtil.removeMe(loc1);
            var loc2;
            var loc3=((loc2 = this)._amount - 1);
            loc2._amount = loc3;
            if (this._amount < 0) 
            {
                this._amount = 0;
            }
            return;
        }

        public updateCD(arg1: CDItemData, arg2: int): void
        {
            if (this._skillItemDict == null) 
            {
                return;
            }
            var loc1=this._skillItemDict[arg2] as ShortcutsItem;
            if (loc1 == null) 
            {
                return;
            }
            loc1.doCoolDown(arg1, true);
            return;
        }

        /* internal  */onClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=(arg1.target as ShortcutsItem).dragSource as SkillInfo;
            if (loc1) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.SkillUseSkill, loc1));
            }
            return;
        }

        /* internal  */onBuffUpdate(arg1: Object): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=false;
            var loc5=0;
            var loc6=this._skillItemDict;
            for(loc3 of loc6) 
            {
                if (loc3 == null) 
                {
                    continue;
                }
                if (!(loc4 = this.getIsNeedDeal(loc3))) 
                {
                    continue;
                }
                loc1 = loc3.dragSource as SkillInfo;
                loc2 = Cache.instance.buff.getBuffInfoById(loc1.additionState);
                if (loc2) 
                {
                    loc3.isUsing = true;
                    continue;
                }
                loc3.isUsing = false;
            }
            return;
        }

        /* internal  */getIsNeedDeal(arg1: ShortcutsItem): Boolean
        {
            if (arg1.dragSource == null) 
            {
                return false;
            }
            return (arg1.dragSource as SkillInfo).skillType == ESkillType._ESkillTypeSwitchBuff;
        }

        public clearSkillList(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=this._skillItemDict;
            for (loc3 in loc5) 
            {
                loc1 = this._skillItemDict[loc3];
                if (loc1 == null) 
                {
                    continue;
                }
                loc2 = loc1.dragSource as SkillInfo;
                if (!(loc2 == null) && loc2.skillType == GameConst.CountryBuffSkillType) 
                {
                    continue;
                }
                if (contains(loc1)) 
                {
                    removeChild(loc1);
                }
                loc1.detory();
                delete this._skillItemDict[loc3];
                var loc6;
                var loc7=((loc6 = this)._amount - 1);
                loc6._amount = loc7;
            }
            if (this._amount <= 0) 
            {
                this._amount = 0;
            }
            else 
            {
                this.resetSkillIconPlace();
            }
            return;
        }

        /* internal  */resetSkillIconPlace(): void
        {
            var loc1=null;
            var loc3=0;
            if (this._skillItemDict == null) 
            {
                return;
            }
            var loc2=[];
            var loc4=0;
            var loc5=this._skillItemDict;
            for(loc1 of loc5) 
            {
                if (loc1 == null) 
                {
                    continue;
                }
                loc2.push(loc1);
            }
            loc2.sort(this.sortItems);
            loc3 = 0;
            while (loc3 < loc2.length) 
            {
                loc1 = loc2[loc3];
                loc1.x = loc3 * 38 + 10;
                ++loc3;
            }
            return;
        }

        /* internal  */sortItems(arg1: ShortcutsItem, arg2: ShortcutsItem): int
        {
            if (arg1.dragSource == null || !(arg1.dragSource is SkillInfo)) 
            {
                return -1;
            }
            if (arg2.dragSource == null || !(arg2.dragSource is SkillInfo)) 
            {
                return 1;
            }
            if ((arg1.dragSource as SkillInfo).skillId <= (arg2.dragSource as SkillInfo).skillId) 
            {
                return -1;
            }
            return 1;
        }

        /* internal */ /* var */_skillItemDict: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_amount: int;
    }
